//
//  StepProgressView.swift
//  JTYP
//
//  Created by Groot on 2017/11/30.
//  Copyright © 2017年 inspur. All rights reserved.
//

class StepProgressView: CircularProgressView {
    
    // 目标步数
    public var target: Int = 0 {
        didSet {
            targetLab.text = "目标\(target)步"
        }
    }
    
    // 当前步数
    public var currentStepNumber: Int = 0 {
        didSet {
            if stepNumberLab.currentValue() < 1 {
                stepNumberLab.animationDuration = 0.5
            }else {
                stepNumberLab.animationDuration = 1.5
            }
            stepNumberLab.countFromCurrentValue(to: CGFloat(currentStepNumber))
            calculatePercentage()
        }
    }
    
    // 显示目标步数
    private lazy var targetLab: UILabel = {
       var lab = UILabel()
        lab.textColor = UIColor.init(white: 0.7, alpha: 1.0)
        lab.font = UIFont.systemFont(ofSize: 16.0)
        return lab
    }()
    
    // 显示当前步数
    private lazy var stepNumberLab: UICountingLabel = {
        var lab = UICountingLabel()
        lab.textColor = .black
        lab.text = "0.0"
        lab.method = .linear
        lab.animationDuration = 0.5
        lab.format = "%d"
        lab.font = UIFont.boldSystemFont(ofSize: 45)
        return lab
    }()
    
    // “步”
    private lazy var stepTipLab: UILabel = {
        var lab = UILabel()
        lab.textColor = UIColor.init(white: 0.7, alpha: 1.0)
        lab.font = UIFont.systemFont(ofSize: 14.0)
        lab.text = "步"
        return lab
    }()
    
    override func customization() {
        addSubview(stepNumberLab)
        stepNumberLab.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(10)
        }
        
        addSubview(targetLab)
        targetLab.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-43)
        }
        
        addSubview(stepTipLab)
        stepTipLab.snp.makeConstraints { (make) in
            make.left.equalTo(stepNumberLab.snp.right)
            make.bottom.equalTo(stepNumberLab.snp.bottom)
        }
    }
    
    func calculatePercentage() {
        if target > 0 {
            percent = CGFloat(currentStepNumber) / CGFloat(target)
        }
    }
}
